package org.lyh.course.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.lyh.base.util.Result;
import org.lyh.course.domain.CourseType;
import org.lyh.course.query.CourseTypeQuery;
import org.lyh.course.service.ICourseTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courseType")
@Api(tags = "课程类型管理")
public class CourseTypeController {
    @Autowired
    private ICourseTypeService courseTypeService;
    @GetMapping
    @ApiOperation("查询所有课程类型")
    Result getDeptList(){
        try {
            return Result.success(courseTypeService.queryList());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("后台未知错误！");
        }

    }
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询一个课程类型")
    Result getDeptOne(@PathVariable("id") Long id){
        CourseType courseType = courseTypeService.queryOne(id);
        return courseType==null ? Result.error("查询的课程类型不存在！"):Result.success(courseType);
    }
    @PutMapping
    @ApiOperation("更新课程类型")
    Result updateDept(@RequestBody CourseType courseType){
        try {
            courseTypeService.updateOne(courseType);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("课程类型更新失败！");
        }
    }
    //添加成功，并返回主键
    @PostMapping
    @ApiOperation("新增课程类型")
    Result addDept(@RequestBody CourseType courseType){
        try {
            courseTypeService.insertOne(courseType);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("课程类型新增失败！");
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation("根据ID删除课程类型")
    Result deleteDept(@PathVariable("id") Long id){
        try {
            courseTypeService.deleteById(id);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("课程类型新增失败！");
        }

    }
    @ApiOperation("根据ids批量删除课程类型")
    @PatchMapping
    Result batchDelete(@RequestBody List<Long> ids){
        try {
            courseTypeService.batchDelete(ids);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("批量删除出错！");
        }
    }
    //高级查询及分页查询
    @ApiOperation("高级查询及分页查询")
    @PostMapping("/query")
    Result query(@RequestBody CourseTypeQuery dto){
        try {
            return Result.success(courseTypeService.queryPage(dto));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询出错！");
        }
    }
    @ApiOperation("获取课程类型树")
    @GetMapping("/tree")
    Result getCourseTypeTree(){
        try {
            return Result.success(courseTypeService.queryTree());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询出错！");
        }
    }
}
